﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    public class AgentDataAccess : BaseDataAccess<Business.Entities.Agent>
    {
        public override GMAC.ReportingSystem.Business.Entities.Agent Get(object id)
        {
            ArrayList parameters = new ArrayList() { id };
            DataTable dt = DataAccessComponent.ExecuteSelect("procAgentsByIdSelect", parameters);
            if (dt != null && dt.Rows.Count > 0)
                return ReadRow(dt.Rows[0]);
            return null;
        }

        public AgentCollection GetList()
        {
            DataTable dt = DataAccessComponent.ExecuteSelect("procAgentsSelect", null);
            return ReadTable(dt) as AgentCollection;
        }

        protected override GMAC.ReportingSystem.Business.Entities.Agent ReadRow(System.Data.DataRow row)
        {
            Agent Agent = null;
            if (row != null)
            {
                try
                {
                    Agent = new Agent();
                    Agent.ID = (int)row["ID"];
                    Agent.UserName = row["UserName"].ToString();
                    Agent.FirstName = row["FirstName"].ToString();
                    Agent.LastName = row["LastName"].ToString();
                    if (!string.IsNullOrEmpty(row["ManagementGroup"].ToString()))
                    {
                       Agent.ManagementGroup =int.Parse(row["ManagementGroup"].ToString());
                    }
                    Agent.Turn = (GeneralEnumTypes.Turn)Enum.Parse(typeof(GeneralEnumTypes.Turn), (row["Turn"].ToString()));
                    if (row["TypeAgent"].ToString() == "True")
                        Agent.TypeAgent = GeneralEnumTypes.TypeAgent.OutBound;
                    else
                        Agent.TypeAgent = GeneralEnumTypes.TypeAgent.Inbound;
                    Agent.Status = bool.Parse(row["Status"].ToString());
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            return Agent;
        }

        protected override List<GMAC.ReportingSystem.Business.Entities.Agent> ReadTable(System.Data.DataTable table)
        {
            try
            {
                AgentCollection Agents = new AgentCollection();
                Agent Agent;
                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        Agent = ReadRow(row);
                        if (Agent != null)
                        {
                            Agents.Add(Agent);
                        }
                    }
                }
                return Agents;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public AgentCollection Search(string UserName, string FirstName, string LastName, int? ManagementGroup, int? Turn, int? TypeAgent, PagingDetails paging)
        {
            ArrayList parameters = new ArrayList() { UserName, FirstName, LastName, ManagementGroup, Turn, TypeAgent,paging.PageNumber, paging.PageSize };

            try
            {
                DataSet ds = DataAccessComponent.ExecuteMultipleSelect("procAgentsByFilterSelect", parameters);
                AgentCollection result = null;
                if (ds.Tables.Count == 2)
                {
                    result = ReadTable(ds.Tables[0]) as AgentCollection;
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        result.TotalResults = (int)ds.Tables[1].Rows[0][0];
                    }
                }
                return result;
            }
            catch (Exception ex)
            {

                throw;
            }

        }
         
    }
}
